<template>
  <div class="question-table-container">
    <!-- 题目表格 -->
    <el-table
      :data="data"
      v-loading="loading"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      :row-class-name="tableRowClassName">
      <el-table-column
        type="selection"
        width="55"
        align="center">
      </el-table-column>
      <el-table-column
        prop="categoryId"
        label="题目分类"
        width="120"
        align="center"
        header-align="center">  
        <template slot-scope="scope">
          <el-tag :type="getTagType(scope.row.categoryId)">
            {{ scope.row.categoryName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="类型"
        width="120"
        align="center"
        header-align="center">
        <template slot-scope="scope">
          <el-tag :type="getTagType(scope.row.type)">{{ scope.row.type }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="questionText"
        label="题目"
        min-width="300"
        align="center"
        header-align="center">
        <template slot-scope="scope">
          <div class="content-cell" v-html="scope.row.questionText"></div>
        </template>
      </el-table-column>
      <el-table-column
        prop="answer"
        label="答案"
        min-width="250"
        align="center"
        header-align="center">
        <template slot-scope="scope">
          <div class="content-cell" v-html="scope.row.answer"></div>
        </template>
      </el-table-column>
      <el-table-column
        prop="score"
        label="分值"
        width="100"
        align="center"
        header-align="center">
      </el-table-column>
      <el-table-column
        prop="answerTime"
        label="答题时间"
        width="100"
        align="center"
        header-align="center">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        fixed="right"
        align="center"
        header-align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pagination.currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'QuestionTable',
  props: {
    data: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    pagination: {
      type: Object,
      required: true
    }
  },
  methods: {
    // 处理选择变化
    handleSelectionChange(selection) {
      this.$emit('selection-change', selection);
    },
    // 处理编辑
    handleEdit(row) {
      this.$emit('edit', row);
    },
    // 处理删除
    handleDelete(row) {
      this.$emit('delete', row);
    },
    // 处理每页条数变化
    handleSizeChange(val) {
      this.$emit('size-change', val);
    },
    // 处理当前页变化
    handleCurrentChange(val) {
      this.$emit('current-change', val);
    },
    // 获取标签类型
    getTagType(type) {
      const typeMap = {
        '选择题': 'primary',
        '填空题': 'success',
        '问答题': 'warning'
      };
      return typeMap[type] || 'info';
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 === 0) {
        return 'warning-row';
      } else {
        return 'success-row';
      }
    }
  }
};
</script>

<style scoped>
.question-table-container {
  width: 100%;
  overflow-x: auto;
}

/* 表格单元格样式 */
/deep/ .el-table .cell {
  white-space: normal !important;
  word-break: break-word !important;
  line-height: 1.5;
  padding: 8px;
}

/* 表头样式 */
/deep/ .el-table th > .cell {
  white-space: nowrap;
  font-weight: bold;
}

/* 内容单元格样式 */
.content-cell {
  text-align: left;
  line-height: 1.6;
}

/* 图片样式 */
.content-cell img {
  max-width: 100%;
  height: auto !important;
  display: block;
  margin: 8px auto;
  border-radius: 4px;
}

/* 表格行悬停效果 */
/deep/ .el-table--enable-row-hover .el-table__body tr:hover > td {
  background-color: #f5f7fa;
}

/* 修复表格边框 */
/deep/ .el-table--border th,
/deep/ .el-table--border td {
  border-right: 1px solid #ebeef5;
}

/* 表格斑马纹 */
/deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: #fafafa;
}

/* 表格行高 */
/deep/ .el-table__body tr,
/deep/ .el-table__body td {
  padding: 8px 0;
}

/* 操作按钮间距 */
.el-button + .el-button {
  margin-left: 8px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
